package com.example.classtest.service;

import com.example.classtest.model.GoodsInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsService {

    private List<GoodsInfo> goods = new ArrayList<>();

    // 判断集合里是否有元素
    private boolean isEmpty(){
        if(goods != null){
            return true;
        }
        return false;
    }

    public List<GoodsInfo> add(GoodsInfo goodsinfo){

            for (GoodsInfo item : goods) {
                if(item.getGid()==goodsinfo.getGid()){
                    item.setGoodsnum(item.getGoodsnum()+1);
                }else {
                    goods.add(goodsinfo);
                }
            }
        return goods;
    }

    public List<GoodsInfo> delete (GoodsInfo goodsinfo){

        if (isEmpty()){
            for (GoodsInfo item : goods) {
                if(item.getGid()==goodsinfo.getGid()){
                    item.setGoodsnum(item.getGoodsnum()-1);
                    return goods;
                }
            }
        }
        return null;
    }

    public double subTotal(){

        double result = 0;

        if(isEmpty()){
            for (GoodsInfo item : goods) {
                result += item.getPrice();
            }
        }
        return result;

    }

    public double total() {

        double result = 0;

        if (isEmpty()) {
            for (GoodsInfo item : goods) {
                result += item.getPrice() + (item.getPrice() * item.getTax());
            }
        }
        return result;

    }


}